﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EQT.Model;

namespace EQT.Dal
{
    public partial class DaKeptLog
    {
        /// <summary>
        /// 分页查询设备保管记录。
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetEquipKeptRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {

            string strGetEmploreeRecords = "select * from V_Equip_Kept ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetEmploreeRecords = strGetEmploreeRecords + condition;
            }

            try
            {
                return DaCommon.GetPaginationData(strGetEmploreeRecords, PageSize, PageIndex, " Kpt_Log_ID desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return new DataSet();
        }

        /// <summary>
        /// 按条件查询设备保管记录。
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataSet GetEquipKeptRecords(string condition)
        {
            string strGetEmploreeRecords = "select * from V_Equip_Kept ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetEmploreeRecords = strGetEmploreeRecords + condition;
            }

            try
            {
                return db.ExecuteDataSet(CommandType.Text, strGetEmploreeRecords);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        /// <summary>
        /// 保管记录登记，除了登记保管记录，还要更改上次的保管状态。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lastKeptID"></param>
        /// <returns></returns>
        public int CreateKeptLog(MoKeptLog entity, string lastKeptID)
        {

            string SQL_UPDATE = "Update kept_log set Kpt_Status=2,Kpt_End=@Kpt_End where Kpt_Log_ID=@Kpt_Log_ID";
            string SQL_INSERT = "INSERT INTO Kept_Log (Equip_ID ,Kpt_Begin ,User_Code1 ,User_Code2 ,Kpt_End ,Kpt_Addr ,Kpt_Status ,Rem ) VALUES "
            + "(@Equip_Id,@Kpt_Begin,@User_Code1,@User_Code2,@Kpt_End,@Kpt_Addr,@Kpt_Status,@Rem)";
            int rtn = 0;

            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

                        //设置参数:Equip_Id	
                        db.AddInParameter(dbCommand, "Equip_Id", DbType.AnsiString, entity.Equip_Id);
                        //设置参数:Kpt_Begin	
                        db.AddInParameter(dbCommand, "Kpt_Begin", DbType.DateTime, entity.Kpt_Begin);
                        //设置参数:User_Code1	
                        db.AddInParameter(dbCommand, "User_Code1", DbType.AnsiString, entity.User_Code1);
                        //设置参数:User_Code2	
                        db.AddInParameter(dbCommand, "User_Code2", DbType.AnsiString, entity.User_Code2);
                        //设置参数:Kpt_End	
                        db.AddInParameter(dbCommand, "Kpt_End", DbType.DateTime, entity.Kpt_End);
                        //设置参数:Kpt_Addr	
                        db.AddInParameter(dbCommand, "Kpt_Addr", DbType.String, entity.Kpt_Addr);
                        //设置参数:Kpt_Status	
                        db.AddInParameter(dbCommand, "Kpt_Status", DbType.Int32, entity.Kpt_Status);
                        //设置参数:Rem	
                        db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);

                        if (!string.IsNullOrEmpty(lastKeptID))
                        {
                            DbCommand dbCommand_Update = db.GetSqlStringCommand(SQL_UPDATE);
                            db.AddInParameter(dbCommand_Update, "Kpt_End", DbType.DateTime, DateTime.Now);
                            db.AddInParameter(dbCommand_Update, "Kpt_Log_ID", DbType.Int32, int.Parse(lastKeptID));

                            db.ExecuteNonQuery(dbCommand_Update, trans);
                        }
                        rtn = db.ExecuteNonQuery(dbCommand, trans);
                        trans.Commit();
                    }
                    cnn.Close();
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rtn;
            
        }

        /// <summary>
        /// 设备的最后一次保管记录
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        public MoKeptLog GetEquipLastKept(string deviceid)
        {
            string condition = " where equip_id='"+deviceid+"' and kpt_status=1 ";
            List<MoKeptLog> result = this.GetEntities(condition);
            if (result.Count > 0)
            {
                return result[0];
            }
            else
            {
                return null;
            }
        }
    }
}
